<template>
  <div id="Detail">
    <!-- 顶部详情导航 -->
    <detail-nav-bar class="detail-nav" @tabClick="tabClick" ref="nav"></detail-nav-bar>
    <!-- 详情页轮播图 -->
     <!-- 滚动 -->
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      :observeImage="true"
      :observeDOM="true"
      @scroll="contentScroll" >
    <myswiper></myswiper>
    <!-- 商品基本信息 -->
    <detail-base-info :goods="goods"></detail-base-info>
    <!-- 店铺信息 -->
    <detail-shop-info :shop="shopInfo"></detail-shop-info>
    <!-- 图片展示 -->
    <detail-goods-info :detailInfo="detailInfo" @imageLoad="imageLoad" class="goods-info"></detail-goods-info>
    <!-- 尺码信息 -->
    <detail-param-info :param-info="paramInfo" ref="params" />
    <!-- 评论信息 -->
    <detail-comment-info :commentInfo="commentInfo" ref="comment"></detail-comment-info>
    <!-- 推荐 -->
    <goods-list :goods="recommends" ref="recommend"></goods-list>
    </scroll>
    <!-- 底部导航 -->
    <detail-bottom-bar class="bottom-bar" @addToCart="addToCart" ></detail-bottom-bar>
    <!-- 返回顶部按钮 -->
    <back-top></back-top>
     <!-- 弹窗 -->
    <toast :message="message" :isShow="isShow" />
  </div>
  
</template>

<script>
// swiper
import Myswiper from "./childComps/myswiper.vue";

import Toast from "../../components/common/toast/Toast.vue"

import DetailNavBar from "@/views/detail/childComps/DetailNavBar";
// 商品信息
import DetailBaseInfo from "./childComps/DetailBaseInfo.vue";
// 店铺信息
import DetailShopInfo from "./childComps/DetailShopInfo.vue";
// 图片展示
import DetailGoodsInfo from "./childComps/DetailGoodsInfo.vue";
//尺码信息
import DetailParamInfo from "./childComps/DetailParamInfo.vue";
// 评论信息
import DetailCommentInfo from "./childComps/DetailCommentInfo.vue";
//推荐
import GoodsList from "../../components/content/goods/GoodsList.vue";
//底部导航
import DetailBottomBar from "./childComps/DetailBottomBar.vue";
// 返回顶部
import BackTop from "../../components/content/backtop/BackTop.vue";
// 滑动
import Scroll from "../../components/common/scroll/Scroll.vue"
//防抖
import {debounce} from "../../common/utils"
import {
  getDetail,
  Goods,
  GoodsParam,
  getRecommend,
} from "../../network/detail";
export default {
  name: "Detail",
  data() {
    return {
      goods: {},
      iid: null,
      topImages: [],
      shopInfo: {},
      detailInfo: {},
      paramInfo: {},
      recommends: [],
      paramInfo: {},
      commentInfo: {},
      product: {},
      themeTopYs: [], // 记录滚动y轴坐标
       getThemeTopY: null, // 防抖函数
       isShow: false,
       currentIndex: 0,
       currentIndex: 0,
       detail_nav: 0,
       message: '',
    };
  },
  components: {
    DetailNavBar,
    Myswiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    BackTop,
    Scroll,
    Toast,
  },
  // mounted() {
  //   //监听滚动条
  //   window.addEventListener("scroll", this.watchScroll);
  // },
  // destroyed() {
  //   //销毁滚动事件
  //   window.removeEventListener("scroll", this.watchScroll);
  // },
  created() {
    //保存传入的iid
    this.iid = this.$route.params.iid;
    // 获取详情页数据
    getDetail(this.iid).then((res) => {
      const data = res.result;
      console.log(data);
      //获取顶部的图片详情数据
      this.topImages = data.itemInfo.topImages;
      // 获取商品信息
      this.goods = new Goods(
        // 商品信息
        data.itemInfo,
        data.columns,
        data.shopInfo.services
      );
      // 获取店铺信息
      this.shopInfo = data.shopInfo;
      // 获取图片信息
      // 5.获取参数的信息
      this.paramInfo = new GoodsParam(
        data.itemParams.info,
        data.itemParams.rule
      );
      this.detailInfo = data.detailInfo;
      //获取评论信息
      if (data.rate.cRate !== 0) {
        this.commentInfo = data.rate.list[0];
      }
    });
    //获取推荐信息
    getRecommend().then((res) => {
      this.recommends = res.data.list;
    });
    //顶部点击页面跳动
    this.$nextTick(() => {
      this.getThemeTopY = debounce(() => {
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop - 44);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 44);
      this.themeTopYs.push(Number.MAX_VALUE); // 存入最大值，用于边界条件判断
      console.log(this.themeTopYs);
    }, 200);
    })
  },
  methods: {
     imageLoad() {
      // this.$refs.scroll.refresh();
      // console.log("------------------------");
      this.getThemeTopY();
    },
    addToCart () {
      this.product.iid = this.iid;
      this.product.img = this.topImages[0];
      this.product.title = this.goods.titlle;
      this.product.desc = this.goods.desc;
      this.product.newPrice = this.goods.realPrice;
      console.log(this.topImages[0]);
      console.log(this.product.title);
      // 添加到购物车
      this.$store.dispatch('addCart',this.product).then(res =>{
        console.log(res);
        this.message = res;
        this.isShow = true;
        setTimeout(() => {
          this.isShow = false;
          this.message = '';
        },1500)
      })
      // console.log(this.$store.state.cartList);
    },
     tabClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200);
    },
    contentScroll(position) {
      const positionY = -position.y;
      const length = this.themeTopYs.length;
      for (let i = 0; i < length - 1; i++) {
        // 判断做法、 hack
        if (
          this.currentIndex !== i &&
          positionY >= this.themeTopYs[i] &&
          positionY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i; // 用于避免重复记录i的次数
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
      // 2.确定BackTop是否显示
      this.isBackShow = (-position.y) > 1000
    },
  },
};
</script>

<style scoped>
#Detail{
  position: relative;
  z-index: 9;
  background-color: #fff;
  height: 100vh;
}
.content {
  height: calc(100% - 102px);
  overflow: hidden;
}
.goods-info {
  position: relative;
}
.detail-nav {
  position: relative;
  z-index: 9;
  background-color: #fff;
  font-weight: 700;
}
.bottom-bar {
  z-index: 99;
  position: fixed;
  bottom: 0;
  height: 70px;
  width: 100%;
  background-color: #fff;
}
</style>